package com.team2.outpatient_sys.web;

import com.team2.outpatient_sys.dao.PatientDao;
import com.team2.outpatient_sys.pojo.*;
import com.team2.outpatient_sys.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/")
public class SignController {

    @Autowired
    private RegisteredService registeredService;

    @Autowired
    private DoctorScheduleService doctorScheduleService;

    @Autowired
    private QueueUpService queueUpService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private CaseHistoryService caseHistoryService;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private PrescriptionService prescriptionService;

    @Autowired
    private PrescriptionMedicineService prescriptionMedicineService;

    /**
     * 签到
     * @param userId
     * @return
     */
    @GetMapping("/sign")
    public Result sign(Long userId){
        Result result = new Result();
        Registered registered = registeredService.findByUserId(userId);
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        int hour = Integer.parseInt(sdf.format(new Date()));
        if(hour!=registered.getTimeQuantumList().get(0)||hour>=registered.getTimeQuantumList().get(1)){
//            if(hour<registered.getTimeQuantum()){
                //用户未在相应预约时间段签到
                SimpleDateFormat format =new SimpleDateFormat("dd");
                int day = Integer.parseInt(format.format(new Date()));
                List<Schedule> schedules = doctorScheduleService.findByDoctorId(Long.valueOf(registered.getDoctorId()));
                List<Integer> days = new ArrayList<>();
                for(Schedule schedule : schedules){
                    days.add(Integer.parseInt(schedule.getShift()));
                }
                if(!days.contains(day)){
                    //用户未在该医生值班期间签到
                    //挂号作废
                    registered.setStatus(4);
                    registeredService.update(registered);
                }else{
                    //生成排号
                    QueueUp queueUp = new QueueUp();
                    queueUp.setDoctorId(registered.getDoctorId());
                    queueUp.setPatientId(registered.getPatientId());
                    queueUp.setQueueNo(registered.getTimeQuantum().toString());
                    //已排号
                    queueUp.setState(0);
                    queueUpService.add(queueUp);
                }
//            }
        }else{
            //生成排号
            QueueUp queueUp = new QueueUp();
            queueUp.setDoctorId(registered.getDoctorId());
            queueUp.setPatientId(registered.getPatientId());
            queueUp.setQueueNo(registered.getTimeQuantum().toString());
            //已排号
            queueUp.setState(0);
            queueUpService.add(queueUp);
        }
        result.setCode(200);
        result.setMsg("签到成功");
        return result;
    }

    /**
     * 查询排号列表
     * @return
     */
    @GetMapping("/queueUpList")
    public Result queueUpList(){
        Result result = new Result();
        List<QueueUp> queueUpList = queueUpService.searchQueueList();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(queueUpList);
        return  result;
    }

    /**
     * 叫号
     * @param id
     * @return
     */
    @GetMapping("/callQueueUp")
    public Result callQueueUp(Long id){
        Result result = new Result();
        QueueUp queueUp = queueUpService.findById(id);
        //叫号中
        queueUp.setState(1);
        queueUpService.update(queueUp);
        result.setCode(200);
        result.setMsg("操作成功");
        return  result;
    }

    /**
     * 叫号成功
     * @param id
     * @return
     */
    @GetMapping("/queueUpSuccess")
    public Result queueUpSuccess(Long id){
        Result result = new Result();
        QueueUp queueUp = queueUpService.findById(id);
        //成功
        queueUp.setState(3);
        queueUpService.update(queueUp);
        result.setCode(200);
        result.setMsg("叫号成功");
        return  result;
    }

    /**
     * 未回应叫号
     * @param id
     * @return
     */
    @GetMapping("/noResponseQueueUp")
    public Result noResponseQueueUp(Long id){
        Result result = new Result();
        QueueUp queueUp = queueUpService.findById(id);
        //未回应
        queueUp.setState(2);
        queueUpService.update(queueUp);
        result.setCode(200);
        result.setMsg("作废成功");
        return  result;
    }

    /**
     * 病人返回叫号
     * @param patientId
     * @return
     */
    @GetMapping("/patientBack")
    public Result patientBack(Long patientId){
        Result result = new Result();
        QueueUp queueUp = queueUpService.findByPatientId(patientId);
        //未回应
        queueUp.setState(2);
        queueUpService.update(queueUp);
        result.setCode(200);
        result.setMsg("操作成功");
        return  result;
    }

    /**
     * 排号取消挂号作废
     * @param patientId
     * @return
     */
    @GetMapping("/cancelQueueUp")
    public Result cancelQueueUp(Long patientId){
        Result result = new Result();
        QueueUp queueUp = queueUpService.findByPatientId(patientId);
        //作废
        queueUp.setState(3);
        queueUpService.update(queueUp);
        //修改挂号状态
        Registered registered = new Registered();
        registered.setPatientId(patientId.intValue());
        registered.setTimeQuantum(Integer.parseInt(queueUp.getQueueNo()));
        registered.setStatus(4);
        registeredService.updateByPatentTime(registered);
        result.setCode(200);
        result.setMsg("操作成功");
        return  result;
    }

    /**
     * 查询未响应列表
     * @return
     */
    @GetMapping("/noQueueList")
    public Result noQueueList(){
        Result result = new Result();
        List<QueueUp> queueUpList = queueUpService.findByNoResponse();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(queueUpList);
        return  result;
    }

    /**
     * 查询患者信息和所有病例
     * @param patientId
     * @return
     */
    @GetMapping("/searchPatientHistory")
    public Result searchPatientHistory(Integer patientId){
        Result result = new Result();
        Patient patient = patientService.findById(patientId);
        //查询历史病例
        List<CaseHistory> caseHistoryList = caseHistoryService.searchCaseHistory(patientId);
        patient.setCaseHistoryList(caseHistoryList);
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(patient);
        return  result;
    }

    /**
     * 追加新病例
     * @param caseHistory
     * @return
     */
    @PostMapping("/appendCaseHistory")
    public Result addCaseHistory(@RequestBody CaseHistory caseHistory){
        Result result = new Result();
        caseHistoryService.add(caseHistory);
        result.setCode(200);
        result.setMsg("追加成功");
        return  result;
    }

    /**
     * 查询药品
     * @return
     */
    @GetMapping("/searchMedicine")
    public Result searchMedicine(){
        Result result = new Result();
        List<PharmacyStorage> pharmacyStorageList = inventoryService.findAllPharmacyStorage();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(pharmacyStorageList);
        return  result;
    }

    /**
     * 开具处方
     * @param prescription
     * @return
     */
    @PostMapping("/prescription")
    public Result prescription(@RequestBody Prescription prescription){
        Result result = new Result();
        //创建处方
        prescriptionService.add(prescription);
        //创建药品和处方
        for(PrescriptionMedicine prescriptionMedicine : prescription.getPrescriptionMedicineList()){
            prescriptionMedicine.setPrescriptionId(prescription.getId());
            prescriptionMedicineService.add(prescriptionMedicine);
        }
        result.setCode(200);
        result.setMsg("操作成功");
        return  result;
    }

    /**
     * 结束就诊
     * @param id
     * @return
     */
    @GetMapping("/doneQueueUp")
    public Result doneQueueUp(Long id){
        Result result = new Result();
        QueueUp queueUp = queueUpService.findById(id);
        //已就诊
        queueUp.setState(4);
        queueUpService.update(queueUp);
        result.setCode(200);
        result.setMsg("操作成功");
        return  result;
    }
}
